/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.jennic.internal;

import java.util.Hashtable;
import java.util.Map;

import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Unbind;
import org.apache.felix.ipojo.annotations.Validate;
import org.osami.gateway.jennic.IJennicDevice;
import org.osami.gateway.jennic.IMessageHandler;
import org.osami.gateway.jennic.IMessageSource;
import org.osami.gateway.jennic.IPayload;
import org.osami.gateway.jennic.IPayloadParser;
import org.osami.gateway.jennic.Message;
import org.osami.gateway.jennic.PayloadParsingException;
import org.osami.gateway.jennic.internal.message.EventPayload;
import org.osgi.framework.ServiceReference;
import org.osgi.service.device.Constants;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 16 juin 2010
 * Time: 18:51:02
 * To change this template use File | Settings | File Templates.
 */
@Component
public class PayloadDispatcher implements IMessageHandler {

    @Requires(filter = "(type=data)")
    private IMessageSource source;

    @Requires
    private IPayloadParser parser;

    private Map<String, IJennicDevice> devices;

    public PayloadDispatcher() {
        this.devices = new Hashtable<String, IJennicDevice>();
    }

    @Validate
    public void start() {
        source.registerHandler(this);
    }

    @Invalidate
    public void stop() {
        if (source != null) {
            source.unregisterHandler(this);
        }
    }

    @Bind(aggregate = true)
    public void bindDevice(IJennicDevice device, ServiceReference reference) {
        String id = (String) reference.getProperty(Constants.DEVICE_SERIAL);
        System.out.println("bindDevice" + reference + " for ID " + id);
        devices.put(id, device);
    }

    @Unbind
    public void unbindDevice(ServiceReference reference) {
        String id = (String) reference.getProperty(Constants.DEVICE_SERIAL);
        System.out.println("unbindDevice" + reference + " for ID " + id);
        devices.remove(id);
    }

    public void handle(Message message) {
        String payloadAsString = message.getPayload();
        // Need to parse payload
        IPayload payload = null;
        try {
            payload = parser.parse(payloadAsString);

            System.out.println("Received payload : " + payload);

            // We will only handle event type message
            if (IPayload.Type.EVENT.equals(payload.getType())) {
                EventPayload event = (EventPayload) payload;

                String nodeId = event.getId();
                System.out.println("Node ID : " + nodeId);

                IJennicDevice selected = devices.get(nodeId);
                if (selected != null) {
                    System.out.println("Device target : " + selected);
                    selected.dispatch(payload);
                }

            }
        } catch (PayloadParsingException e) {
            e.printStackTrace();
        }
    }
}
